using System;
using System.Collections.Generic;
using System.Management;
using Sedge.Core.Execution.ReportElements;
using Sedge.Core.Globalization;

namespace Sedge.Core.Configuration.Report
{
	public class SystemSource : IDataSource
	{
		private Suite _suite;

		public string Caption { get; set; }
		public Options Options { get; set; }

		public void PrepareSource(Suite suite)
		{
			_suite = suite;
		}

		public List<IReportElement> GetReportElements()
		{
			ReportTable tableSystem = GetSystemTable();
			ReportTable tableProcessor = GetProcessorTable();
			List<IReportElement> reportElements = new List<IReportElement> { tableSystem, tableProcessor };

			List<IReportElement> drives = GetDrives();
			reportElements.AddRange(drives);

			return reportElements;
		}

		private static string ToMBSize(object size, double divider)
		{
			string result;
			try
			{
				UInt64 intSize = Convert.ToUInt64(size);
				result = String.Format("{0:0.00} MB", intSize / 1024.0 / divider);
			}
			catch
			{
				result = Convert.ToString(size);
			}

			return result;
		}

		private ReportTable GetSystemTable()
		{
			ReportTable table = new ReportTable(_suite) {Header = Caption};
			ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
			foreach (ManagementObject item in searcher.Get())
			{
				try
				{
					table.AddRow(Locale.TemplateStrings.SystemCaption, item["Caption"].ToString());
					try { table.AddRow(Locale.TemplateStrings.SystemArchitecture, item["OSArchitecture"].ToString()); } catch {}

					table.AddRow(Locale.TemplateStrings.SystemVersion, item["Version"].ToString());
					table.AddRow(Locale.TemplateStrings.SystemBuild, item["BuildNumber"].ToString());
					table.AddRow(Locale.TemplateStrings.SystemOSLanguage, item["OSLanguage"].ToString());
					table.AddRow(Locale.TemplateStrings.SystemServicePack, String.Concat(item["ServicePackMajorVersion"].ToString(), ".", item["ServicePackMinorVersion"].ToString()));

					table.AddRow(Locale.TemplateStrings.SystemTotalVM, ToMBSize(item["TotalVirtualMemorySize"], 1));
					table.AddRow(Locale.TemplateStrings.SystemFreeVM, ToMBSize(item["FreeVirtualMemory"], 1));
					table.AddRow(Locale.TemplateStrings.SystemFreePhysicalMemory, ToMBSize(item["FreePhysicalMemory"], 1));
				}
				catch
				{
				}
			}
			return table;
		}

		private ReportTable GetProcessorTable()
		{
			ReportTable table = new ReportTable(_suite);
			ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
			foreach (ManagementObject item in searcher.Get())
			{
				try
				{
					table.AddRow(Locale.TemplateStrings.SystemProcessor, item["Name"].ToString());
					table.AddRow(Locale.TemplateStrings.SystemProcessorCores, item["NumberOfCores"].ToString());
					table.AddRow(Locale.TemplateStrings.SystemProcessorLoad, item["LoadPercentage"].ToString());
				}
				catch
				{
				}
			}
			return table;
		}

		private List<IReportElement> GetDrives()
		{
			List<IReportElement> list = new List<IReportElement>();
			ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
			foreach (ManagementObject item in searcher.Get())
			{
				try
				{
					string name = item["Name"].ToString();
					ReportTable table = new ReportTable(_suite) {Header = name};
					
					table.AddRow(Locale.TemplateStrings.SystemDriveName, name);
					table.AddRow(Locale.TemplateStrings.SystemDriveFileSystem, item["FileSystem"].ToString());
					table.AddRow(Locale.TemplateStrings.SystemDriveSize, ToMBSize(item["Size"], 1024));
					table.AddRow(Locale.TemplateStrings.SystemDriveFreeSpace, ToMBSize(item["FreeSpace"], 1024));
					
					list.Add(table);
				}
				catch
				{
				}
			}
			return list;
		}

	}
}